package com.tfswx.ruleengine.fn;

import com.alibaba.fastjson.JSON;
import com.greenpineyu.fel.context.FelContext;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ricky
 * @date 2021/10/12
 */
public class ArrEq extends BaseFn {
    @Override
    public Object call(Object[] arguments, FelContext context) {
        Object[] arr1 = (Object[]) arguments[0];
        Object[] arr2 = (Object[]) arguments[1];
        if (arr1.length != arr2.length) {
            return false;
        }
        List<Object> newArr1 = Arrays.asList(arr1).stream().sorted().collect(Collectors.toList());
        List<Object> newArr2 = Arrays.asList(arr2).stream().sorted().collect(Collectors.toList());
        return JSON.toJSONString(newArr1).equals(JSON.toJSONString(newArr2));
    }

    @Override
    public String getZwmc() {
        return "判断数组是否相等";
    }


    @Override
    public String getName() {
        return "arrEq";
    }


    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("arr1"));
        params.add(new Param("arr2"));
        return params;
    }
}
